package com.team9.fitness.mapper;

import com.team9.fitness.entity.FoodRecordItem;
import com.team9.fitness.entity.UserFoodRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserFoodRecordMapper {

    @Insert("INSERT INTO user_food_records (user_id, record_date, total_calories) " +
            "VALUES (#{userId}, #{recordDate}, #{totalCalories})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserFoodRecord record);

    @Insert({
            "<script>",
            "INSERT INTO user_food_record_items (record_id, food_name, weight, calories) VALUES ",
            "<foreach collection='items' item='item' separator=','>",
            "(#{recordId}, #{item.foodName}, #{item.weight}, #{item.calories})",
            "</foreach>",
            "</script>"
    })
    int insertRecordItems(@Param("recordId") Long recordId,
                          @Param("items") List<FoodRecordItem> items);

    @Select("SELECT * FROM user_food_records WHERE user_id = #{userId} " +
            "ORDER BY record_date DESC, created_at DESC")
    List<UserFoodRecord> selectByUserId(Long userId);

    @Select("SELECT * FROM user_food_records WHERE user_id = #{userId} " +
            "AND record_date = #{date}")
    UserFoodRecord selectByUserIdAndDate(@Param("userId") Long userId,
                                         @Param("date") String date);

    @Select("SELECT uri.* FROM user_food_record_items uri " +
            "WHERE uri.record_id = #{recordId}")
    List<FoodRecordItem> selectItemsByRecordId(Long recordId);
}
